package com.kkm.controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

public class BaseServlet extends HttpServlet {
    //重写service,进行方法的重新分发
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //设置编码
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        //获取方法名
        String method_name = req.getParameter("method");
        System.out.println(method_name);
        System.out.println(this);
        try {
            Method method = this.getClass().getMethod(method_name, HttpServletRequest.class, HttpServletResponse.class);
            method.setAccessible(true);
            if(method!=null)
                method.invoke(this,req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
